package com.oss.service.oss;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oss.entity.oss.DividendManage;
import com.oss.entity.oss.Student;
import com.oss.enums.IsDeletedEnum;
import com.oss.mapper.oss.DividendManageMapper;
import com.oss.service.system.UserService;
import com.oss.util.SnowFlakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author chengqiang
 */
@Service
public class DividendManageService extends ServiceImpl<DividendManageMapper, DividendManage> {
    @Autowired
    UserService userService;

    public List<DividendManage> selectByList(DividendManage dto) {
        LambdaQueryWrapper<DividendManage> wrapper = new LambdaQueryWrapper<DividendManage>();
        wrapper.eq(DividendManage::getIsDeleted, IsDeletedEnum.NO.getFlag());
        if (dto.getDateStart() != null){
            wrapper.ge(DividendManage::getDateStart, dto.getDateStart());
        }
        if (dto.getDateEnd() != null){
            wrapper.le(DividendManage::getDateEnd, dto.getDateEnd());
        }
        List<DividendManage> list = this.list(wrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            Set<String> ids = list.stream().map(DividendManage::getCreatorId).collect(Collectors.toSet());
            ids.addAll(list.stream().map(DividendManage::getUpdatorId).collect(Collectors.toSet()));
            Map<String,String> userMap = userService.selectByIds(ids);
            for (DividendManage entity : list) {
                entity.setCreatorName(userMap.get(entity.getCreatorId()));
                entity.setUpdatorName(userMap.get(entity.getUpdatorId()));
            }
        }
        return list;
    }

    public String add(DividendManage dto) {
        dto.setId(SnowFlakeUtil.getID());
        this.baseMapper.insert(dto);
        return "操作成功";
    }
    public String updateData(DividendManage dto) {
        this.baseMapper.updateById(dto);
        return "操作成功";
    }

    public String delete(String id) {
        LambdaUpdateWrapper<DividendManage> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(DividendManage::getId,id);
        wrapper.set(DividendManage::getIsDeleted,IsDeletedEnum.YES.getFlag());
        this.update(null,wrapper);
        return "操作成功";
    }
}
